package com.wis.bolt.risk;

import com.wis.TopologyDrive;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2018/12/17 10:47
 * @Modified By:
 */
public class RiskShouJiBolt extends BaseRichBolt {
    private OutputCollector collector;
    private static Queue tuplePpQueue = new ConcurrentLinkedQueue();
    private static Queue tupleVbQueue = new ConcurrentLinkedQueue();
    private static Queue tupleWsQueue = new ConcurrentLinkedQueue();
    private static Queue tupleAtQueue = new ConcurrentLinkedQueue();
    private static Queue tupleRiQueue = new ConcurrentLinkedQueue();
    private static Queue tupleXtQueue = new ConcurrentLinkedQueue();
    private static Queue tupleIdQueue = new ConcurrentLinkedQueue();
    private static String typeProduce;

    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        typeProduce = map.get(TopologyDrive.TYPE_PRODUCE).toString();
    }

    @Override
    public void execute(Tuple tuple) {
        String key = tuple.getStringByField("key");
        String njd = tuple.getStringByField("nengjiandu");
        try {
            if (StringUtils.isNotBlank(key)) {
                if (njd.equals("over")) {
                    if (tupleVbQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "2" + key + "00", StringUtils.join(tupleVbQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleWsQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "3" + key + "00", StringUtils.join(tupleWsQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tuplePpQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "1" + key + "00", StringUtils.join(tuplePpQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleAtQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "5" + key + "00", StringUtils.join(tupleAtQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    if (tupleRiQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "4" + key + "00", StringUtils.join(tupleRiQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    tuplePpQueue.clear();
                    tupleWsQueue.clear();
                    tupleVbQueue.clear();
                    tupleAtQueue.clear();
                    tupleRiQueue.clear();
                    tupleIdQueue.clear();
                    tupleXtQueue.clear();

                } else if (njd.equals("jies")) {
                    if (tupleVbQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "2" + key + "00", StringUtils.join(tupleVbQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("1" + typeProduce + "2" + key + "00", "jies", "jies", "jies"));

                    if (tupleWsQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "3" + key + "00", StringUtils.join(tupleWsQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("1" + typeProduce + "3" + key + "00", "jies", "jies", "jies"));

                    if (tuplePpQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "1" + key + "00", StringUtils.join(tuplePpQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("1" + typeProduce + "1" + key + "00", "jies", "jies", "jies"));

                    if (tupleAtQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "5" + key + "00", StringUtils.join(tupleAtQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("1" + typeProduce + "5" + key + "00", "jies", "jies", "jies"));

                    if (tupleRiQueue.size() > 0)
                        this.collector.emit(new Values("1" + typeProduce + "4" + key + "00", StringUtils.join(tupleRiQueue, "=")
                                , StringUtils.join(tupleIdQueue, "="), StringUtils.join(tupleXtQueue, "=")));
                    this.collector.emit(new Values("1" + typeProduce + "4" + key + "00", "jies", "jies", "jies"));

                    tuplePpQueue.clear();
                    tupleWsQueue.clear();
                    tupleVbQueue.clear();
                    tupleAtQueue.clear();
                    tupleRiQueue.clear();
                    tupleIdQueue.clear();
                    tupleXtQueue.clear();
                } else {
                    // TODO 这儿的字段为null，好像没必要去做判断
                    String id = tuple.getStringByField("id");
                    if (StringUtils.isNotBlank(id)) {
                        if (typeProduce.equals("3")) {
                            tupleIdQueue.add(id);
                            tupleVbQueue.add(njd.split(",", 2)[1]);
                            String fs = tuple.getStringByField("fengsu");
                            tupleWsQueue.add(fs.split(",", 2)[1]);
                            String jsl = tuple.getStringByField("jiangshuiliang");
                            tuplePpQueue.add(jsl.split(",", 2)[1]);
                            String qw = tuple.getStringByField("qiwen");
                            tupleAtQueue.add(qw.split(",", 2)[1]);
                            String jb = tuple.getStringByField("daolujiebing");
                            tupleRiQueue.add(jb.split(",", 2)[1]);
                            String jsxt = tuple.getStringByField("jiangshuixiangtai");
                            tupleXtQueue.add(jsxt.split(",", 2)[1]);
                        } else {
                            tupleIdQueue.add(id);
                            tupleVbQueue.add(njd);
                            String fs = tuple.getStringByField("fengsu");
                            tupleWsQueue.add(fs);
                            String jsl = tuple.getStringByField("jiangshuiliang");
                            tuplePpQueue.add(jsl);
                            String qw = tuple.getStringByField("qiwen");
                            tupleAtQueue.add(qw);
                            String jb = tuple.getStringByField("daolujiebing");
                            tupleRiQueue.add(jb);
                            String jsxt = tuple.getStringByField("jiangshuixiangtai");
                            tupleXtQueue.add(jsxt);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.collector.ack(tuple);
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("key", "yaosu", "id", "xt"));
    }
}
